<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>

</body>
</html>
<script>
  // 1.唐泽
  function getStrLong(str) {
    let arr = str.split('');
    let stack = [];
    let max = {
      len: 0,
      arrs: []
    };
    for (let i = 0; i <= arr.length; i++) {
      if (stack[0] !== arr[i]) {
        stack.length > max.len ? (max.arrs = stack, max.len = stack.length) : null;
        stack = [];
        stack.push(arr[i])
      } else {
        stack.push(arr[i])
      }
    }
    console.log(max.arrs.join(''));
  }

  getStrLong('wqehwuehshhshshshhh');

  // 2. 李凤
  function size(str) {
    if (str.indexOf("_") > -1) {
      let a = str.toLowerCase()
      let b = a.replace(a[a.indexOf("_") + 1], a[a.indexOf("_") + 1].toUpperCase());
      let c = b.split("_").join("")
      return c
    } else {
      let d = ""
      console.log(str[0].toUpperCase());
      if (str[0] === str[0].toUpperCase()) {
        d = str[0].toLowerCase() + str.slice(1)
      }
      return d;
    }
  }

  // size("TestVariable")
  // 3. 杨晓研
  function fn(str) {
    let len = str.length
    let newStr = ''
    for (let i = 0; i < len; i++) {
      let flag = 1
      for (let j = 0; j < len; j++) {
        if (i === j) {
          continue
        }
        if (str[j] === str[i]) {
          flag = 0
        }
      }
      if (flag === 1) {
        newStr = str[i]
      }
    }
    return newStr
  }

  console.log(fn('shopee'))
  console.log(fn('shope'))
  console.log(fn('shoppee'))

  // 4.杨云涵
  function permute(num) {
    let len = num.length
    let arr = []
    let res = []
    let back = (arr) => {
      if (arr.length === len) {
        res.push(arr)
        return
      }
      for (let i = 0; i < num.length; i++) {
        if (!arr.includes(num[i])) {
          arr.push(num[i])
          back(arr.slice())
          arr.pop()
        }
      }
    };
    back(arr);
    return res
  }

  console.log(permute([1, 2, 3]));

  // 5. 王梦江
  function fun(num) {
    for (let i = 1; i <= num; i++) {
      if (i % 3 === 0) {
        console.log('Fizz');
      } else if (i % 5 === 0) {
        console.log('Buzz');
      } else if (i % 5 === 0 && i % 3 === 0) {
        console.log('FizzBuzz');
      } else {
        console.log(i);
      }
    }
  }

  fun(15)

  //6.王佳鑫
  function addTwoNumbers(l1, l2, curr = 0) {
    if (l1 === null && l2 === null) {
      if (curr) return new ListNode(curr)
      else return null;
    } else {
      if (l1 == null) l1 = new ListNode(0)
      if (l2 == null) l2 = new ListNode(0)
      let nextVal = (l2.val || 0) + (l1.val || 0) + curr
      curr = 0
      if (nextVal > 9) {
        curr = 1
        nextVal -= 10
      }
      l1.val = nextVal
      l1.next = addTwoNumbers(l1.next, l2.next, curr)
      return l1
    }
  }

  // 7.孟轩槿
  function hui(head) {
    let a = ''
    let b = ''
    while (head) {
      a = a + head.val
      b = head.val + b
      head = head.next
    }
    console.log(a === b, '............');
  }

  hui([1, 2, 2, 1])

  //11 陈希然
  function money(n) {
    let count = 1
    while (true) {
      if ((1 + count) * (count / 2) > n) {
        return count - 1
      }
      count++
    }
  }

  console.log(money(20));

  // 8 董臻
  class TreeNode {
    constructor(val, left, right) {
      this.val = val;
      this.left = left;
      this.right = right;
    }
  }
  let asArray = function (nums) {
    function build(arr, left, right) {
      if (left > right) {
        return null
      }
      let mid = Math.ceil((left + right) / 2)
      let root = new TreeNode(arr[mid])
      root.left = build(arr, left, mid - 1)
      root.right = build(arr, mid + 1, right)
      return root
    }

    return build(nums, 0, nums.length - 1)
  };

  const asRoot = (root) => {
    if (!root) {
      return
    }
    const queue = [root]
    let res = []
    while (queue.length) {
      let n = queue.shift()
      res.push(n)
      n.left && queue.push(n.left)
      n.right && queue.push(n.right)
    }
    return res
  };
  console.log(asArray([1, 2, 3, 4, 5, 6, 7]));
  console.log(asRoot(asArray([1, 2, 3, 4, 5, 6, 7])));
  //9 贾志磊
  let root1 = new TreeNode(1, null, null)
  let tree_2 = new TreeNode(2, null, null)
  let tree_3 = new TreeNode(3, null, null)
  let tree_4 = new TreeNode(4, null, null)
  let tree_5 = new TreeNode(5, null, null)
  root1.left = tree_2
  root1.right = tree_3
  tree_2.left = tree_4
  tree_2.right = tree_5
  let root2 = new TreeNode(1, null, null)
  let m2 = new TreeNode(2, null, null)
  let m3 = new TreeNode(3, null, null)
  let m4 = new TreeNode(4, null, null)
  let m5 = new TreeNode(5, null, null)
  root2.left = m2
  root2.right = m3
  m2.left = m4
  m2.right = m5
  console.log(root1, root2);

  function isTree(root1, root2) {
    if (!root1 || !root2) return false
    let stack1 = [root1]
    let stack2 = [root2]
    while (stack1.length && stack2.length) {
      let n = stack1.pop()
      let m = stack2.pop()
      if (n.val !== m.val) {
        return false
      }
      n.left && stack1.push(n.left)
      n.right && stack1.push(n.right)
      m.left && stack2.push(m.left)
      m.right && stack2.push(m.right)
    }
    return true
  }

  console.log(isTree(root1, root2));
  // 闫清倩
  // 12. 实现如下程序
  function TaskList(demarks, time) {
    let obj = {
      demarks,
      taskArr: []
    };

    obj.__proto__ = TaskList.prototype;

    let task = () => {
      return new Promise((resolve, reject) => {
        resolve();
      }).then(() => {
        obj.mySleep(time)
        console.log(demarks);
      })

    }
    obj.taskArr.push(task);

    setTimeout(() => {
      obj.taskArr.forEach(fn => {
        fn();
      })
    });
    return obj;
  }

  TaskList.prototype.sleepAfter = function (time) {
    let task = () => {
      setTimeout(() => {
        this.mySleep(time);
        console.log(`再睡${time}ms`);
      }, 0);
    };
    this.taskArr.push(task);
    return this;
  };
  TaskList.prototype.sleepBefore = function (time) {
    let task = () => {
      // setTimeout(() => {
      this.mySleep(time);
      console.log(`先睡${time}ms`);
      // }, 0);
    }
    this.taskArr.push(task);
    return this;
  }
  TaskList.prototype.execute = function (content) {
    let task = () => {
      setTimeout(() => {
        console.log(content);
      }, 0);
    };
    this.taskArr.push(task);
    return this;
  };
  TaskList.prototype.mySleep = function (time) {
    let startTime = new Date().getTime();
    let endTime = new Date().getTime();
    while (endTime - startTime <= time) {
      endTime = new Date().getTime();
    }
  };
  console.log(TaskList('开始执行任务')
    .sleepAfter(1000)
    .execute('任务完成')
    .sleepBefore(2000));

  // taskList('开始执行任务').sleepAfter(1000).execute('任务完成').sleepBefore(2000).appendChild(2000);

  //13
  const res = {
    code: 0,
    data: {
      lines: '20路,301路,5路,地铁5号线,地铁7号线,机场大巴线,107路,机场快轨',
      lineids: 'lzbd,lwes,lxid,lwic,lwdf,ldfx,lwics,loin',
      linedetails: {
        lwdf: {
          name: '机场大巴线'
        },
        lwes: {
          name: '301路'
        },
        lwic: {
          name: '地铁5号线'
        },
        ldfx: {
          name: '107路'
        },
        lzbd: {
          name: '20路'
        },
        lxid: {
          name: '5路'
        },
        lwics: {
          name: '地铁7号线'
        },
        loin: {
          name: '机场快轨'
        }
      }
    }
  };

  function trip(res) {
    console.log(res.data.linedetails);
    let linesArr = res.data.lines.split(',');// 数组
    let busArr = linesArr.filter(item => item.includes('路'));
    let metroArr = linesArr.filter(item => item.includes('地铁'));
    let airportArr1 = linesArr.filter(item => item.includes('机场快轨'));
    let airportArr2 = linesArr.filter(item => item.includes('机场大巴'));
    busArr.sort((a, b) => {
      return parseInt(a) - parseInt(b)
    });
    metroArr.sort((a, b) => {
      return parseInt(a.slice(2)) - parseInt(b.slice(2))
    });
    let newArr = [...busArr,...metroArr,...airportArr1,...airportArr2]
    // console.log(newArr)

    let arr = []
    let lineDetails = res.data.linedetails
    newArr.forEach(item => {
      // console.log(item)
      for (const lineKey in lineDetails) {
        if (lineDetails[lineKey].name === item) {
          // console.log(lineKey,lineDetails[lineKey])
          let obj = {
            [lineKey]: lineDetails[lineKey]
          };
          arr.push(obj)
        }

      }
    });
    // console.log(arr)
    return arr
  }
  console.log(trip(res));
</script>
